import 'package:flutter/material.dart';
import 'package:yuan_hang_app/base/base_state.dart';
import 'package:yuan_hang_app/base/full_base_page.dart';
import 'package:yuan_hang_app/views/home/model/leads_call_log.dart';
import 'package:yuan_hang_app/views/home/view/leads/widget/leads_call_log_list.dart';
import 'package:yuan_hang_app/views/home/view/leads/widget/leads_history_list.dart';
import 'package:yuan_hang_app/views/home/view/leads/widget/leads_info_list.dart';

import '../../../../base/base_page.dart';
import '../../../../http/api.dart';
import '../../../../http/http_utils.dart';
import '../../../../widget/common_attachment.dart';
import '../../../../widget/custom_tab.dart';
import '../../../../widget/w_toast.dart';
import '../../model/leads.dart';
import '../../model/leads_history.dart';


class LeadsAssignDetailPage extends StatefulWidget {
  final int clueId;
  const LeadsAssignDetailPage({super.key, required this.clueId});

  @override
  BaseState<LeadsAssignDetailPage> createState() => _LeadsAssignDetailPageState();

}

class _LeadsAssignDetailPageState extends BaseState<LeadsAssignDetailPage> {

  List<Map> tabList = [
    {"title": "基本信息"},
    {"title": "接触历史"},
    {"title": "通话记录"}
  ];

  Leads leadsInfo = Leads();
  GlobalKey<CommonAttachmentState>? picKey;
  List<LeadsHistory> leadsHistoryList = [];
  List<LeadsCallLog> leadsCallLogList = [];
  Status historyListStatus = Status.NONE;
  Status callLogListStatus = Status.NONE;
  int selectedTabIndex = 0;

  @override
  void initState() {
    super.initState();
    picKey = GlobalKey(debugLabel: "picKey");
    fetchDataInfo(widget.clueId);
    fetchHistoryList(widget.clueId);
    fetchCallLogList(widget.clueId);
  }

  fetchDataInfo(int clueId) {
    HttpUtils().get("${Api.getClueDetail}/${clueId}").then((value) {
      if (value.resultCode == 200) {
        if(value.data != null) {
          leadsInfo = Leads.fromJson(value.data);
          List picUrlList = (leadsInfo.filesList ?? [])
              .map((item) => item["url"])
              .toList();
          picKey!.currentState?.refreshList(picUrlList);
          setState(() {});
        }
      } else {
        Toast.toast(value.errMsg);
      }
    });
  }

  fetchHistoryList(int clueId) {
    showLoading();
    HttpUtils().get("${Api.getContactList}/${clueId}").then((value) {
      hideLoading();
      if (value.resultCode == 200) {
        if (value.data != null) {
          List data = value.data;
          if (data.isNotEmpty) {
            for (int i = 0; i < data.length; i++) {
              leadsHistoryList.add(LeadsHistory.fromJson(data[i]));
            }
          } else {
            historyListStatus = Status.EMPTY;
          }
        }
      } else {
        historyListStatus = Status.ERROR;
      }
    });
  }

  fetchCallLogList(int clueId) {
    showLoading();
    HttpUtils().get("${Api.getPhoneList}/${clueId}").then((value) {
      hideLoading();
      if (value.resultCode == 200) {
        if (value.data != null) {
          List data = value.data;
          if (data.isNotEmpty) {
            for (int i = 0; i < data.length; i++) {
              leadsCallLogList.add(LeadsCallLog.fromJson(data[i]));
            }
          } else {
            callLogListStatus = Status.EMPTY;
          }
        }
      } else {
        callLogListStatus = Status.ERROR;
      }
    });
  }


  @override
  FullBasePage buildState(BuildContext context) {
    return FullBasePage(
        title: "线索详情",
        child: Column(
          children: [
            CustomTab(
              tabList: tabList,
              tabClick: (index) {
                setState(() {
                  selectedTabIndex = index;
                });
              },
            ),
            Expanded(child: _renderTabByIndex(selectedTabIndex))
          ],
        )
    );
  }


  _renderTabByIndex(index) {
    if(index == 0) {
      return LeadsInfoList(leads: leadsInfo, picKey: picKey!,);
    } else if(index == 1) {
      return LeadsHistoryList(historyList: leadsHistoryList, status: historyListStatus,);
    } else {
      return LeadsCallLogList(logList: leadsCallLogList, status: callLogListStatus,);
    }
  }
}